package com.cupdata.helloLucene;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;

/**   
 * @Title: HelloDirectory.java 
 * @Description: TODO
 * @author dongliangwu
 * @date 2017年2月21日
 * @version V1.0   
 */
public class HelloLucene {
  /**
   * 建立索引
   */
	public void index() {
		IndexWriter iw = null;
		try {
//	 1.创建Directory
//		Directory dic = new RAMDirectory();//索引建立在内存中
		Directory dic = FSDirectory.open(new File("D:/lucene/index01"));
//	 2.创建IndexWriter
		IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35));
			iw = new IndexWriter(dic, iwc);
//	 3.创建Document对象
			Document doc;
			File files = new File("D:/lucene/example");
			for(File file:files.listFiles()) {
				doc = new Document();
				doc.add(new Field("content", new FileReader(file)));
//				Field.Store 是否需要把文件的全名存储到文件中
//				Field.Index 是否需要把文件的全名进行分词，对文件名没有必要进行分词
				doc.add(new Field("filename", file.getName(), Field.Store.YES, Field.Index.NOT_ANALYZED));
				doc.add(new Field("path", file.getAbsolutePath(), Field.Store.YES, Field.Index.NOT_ANALYZED));
//				将文档添加到索引中
				iw.addDocument(doc);
			}
		} catch (CorruptIndexException e) {
			e.printStackTrace();
		} catch (LockObtainFailedException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if(iw!=null)
				try {
					iw.close();
				} catch (CorruptIndexException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				} 
		}
	}
	
	/**
	 * 搜索
	 */
	public void searcher() {
		try {
			//1.创建Directory
			Directory directory = FSDirectory.open(new File("D:/lucene/index01"));
			//2.创建reader
			IndexReader reader = IndexReader.open(directory);
			//3.根据IndexReader创建IndexSearcher
			IndexSearcher searcher = new IndexSearcher(reader);
			//4.创建搜索的Query
			QueryParser queryParser = new QueryParser(Version.LUCENE_35, "content", new StandardAnalyzer(Version.LUCENE_35));
			//创建Query，表示搜索域中包含content中包含“java”的文档
			Query query = queryParser.parse("zhangsan");
			//5.根据searcher搜索并且返回TopDocs
			TopDocs  tds = searcher.search(query, 10);
			//6.根据TopDocs获取文档的评分对象,
			ScoreDoc[] sd = tds.scoreDocs;
			for (ScoreDoc scoreDoc : sd) {
			//7.根据seacher和ScoreDoc对象获取具体的Document对象
			Document d = searcher.doc(scoreDoc.doc);
			//8.根据Document对象获取需要的值
			System.out.println(d.get("filename")+"["+d.get("path")+"]");
			}
			//9.关闭reader
			reader.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
}
